import java.awt.Point;

public class Building {

	private float residents = 0;
	// private Main.Buildings convertingTo;
	private Buildings structure;
	private Point position;
	private Main.Player owner;
	private Buildings prototype = new House();

	// prototype "knows" buildLevel already
	// OR no prototype, just a public enum...
	public Building(Buildings prototype, float residents, Point pos,
			Main.Player p) {
		structure = prototype.clone();
		this.residents = residents;
		position = pos;
		owner = p;
	}

	// ---Population alter methods ----------------------
	public void reinforce() {
		residents++;
	}

	public void setResidents(float n) {
		residents = n;
	}

	public int getResidents() {
		return (int) residents;
	}

	public void growth() {
		residents += structure.growth(residents);
	}

	public boolean defend(byte attackMorale, byte ownMorale, byte attackArmory) {
		residents -= structure.defend(attackMorale, ownMorale, attackArmory);
		return (residents > 0); // if successful defense, residents > 0
	}

	// ---Convert methods ------------------------------
	// this or public enum with switch, case statements.
	public int convertTo(Buildings prototype) {
		this.prototype = prototype;
		return 5000; // time to convert;
	}

	public void convertDone() {
		if (prototype != null) {
			structure = prototype;
			prototype = null;
		}
	}

	public boolean isConvertableTo(Buildings p) {
		return (prototype == null) && (p.getClass().equals(structure));
	}

	// ---Upgrade methods ------------------------------
	public boolean isUpgradeable() {
		return structure.isUpgradeable(residents);
	}

	// ---Attack ---------------------------
	/*
	 * // A line of units marching check if tower is in range and if tower is
	 * free to attack.
	 */
	public void inRange(Unit u) {
		if (structure instanceof Tower)
			structure.InRange(position, u);
	}

	public void overTake(Main.Player p) {
		residents = 0;
		setOwner(p);
		prototype = null; //cancel upgrade, needs more outside help
	}

	public Main.Player getOwner() {
		return owner;
	}

	private void setOwner(Main.Player owner) {
		this.owner = owner;
	}

	public boolean isArmory() {
		return (structure instanceof Armory);
	}
}
